/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProjectExplorer.java
 *
 * Created on 9/10/2009, 12:37:37 PM
 */

package pradoeditor.projectexplorer;


import com.vlsolutions.swing.docking.DockKey;
import com.vlsolutions.swing.docking.Dockable;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import pradoeditor.comun.ItemProject;
import pradoeditor.comun.interfaces.Tree;
import pradoeditor.comun.ui.IconNodeProject;
import pradoeditor.comun.ui.IconNodeRenderer;
import pradoeditor.projectexplorer.menu.PopupMenu;
import pradoeditor.projectexplorer.menu.PopupMenuFactory;
import pradoeditor.resources.text.MessagesCodes;
import pradoeditor.resources.text.MessagesManager;

/**
 *
 * @author jaimeg
 */
public class ProjectExplorer extends JPanel implements Dockable, Tree
{
    DockKey key = new DockKey("ProjectExplorer");
    ItemProject itemProject;
    //DefaultTreeModel modelo;
    //DefaultMutableTreeNode nodeTop;

    List<IconNodeProject> nodes = new ArrayList<IconNodeProject>();
    IconNodeProject nodeTop;

    /** Creates new form ProjectExplorer */
    public ProjectExplorer()
    {
        initComponents();
        initTree();

        key.setName(LABEL_TITLE);
        key.setMaximizeEnabled(false);
    }

    public void setData(ItemProject itemProject)
    {
        this.itemProject = itemProject;
        
        nodes.clear();
        nodeTop = new IconNodeProject(LABEL_PROJECT, true, new javax.swing.ImageIcon(getClass().getResource("/pradoeditor/resources/icons/NewProject.png")));
        nodes.add(nodeTop);

        for(ItemProject i : itemProject.getChildrens())
        {
            boolean hasChildren = i.getChildrens().size() >0 ;
            IconNodeProject newNode = new IconNodeProject(i, hasChildren, new javax.swing.ImageIcon(getClass().getResource("/pradoeditor/resources/icons/SaveAll.png")));
            nodeTop.add(newNode);
            
            fillChildrens(i, newNode);
        }

        treeProject = new JTree( nodes.get(0));
        treeProject.setCellRenderer(new IconNodeRenderer());
        treeProject.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeProjectMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(treeProject);
    }

    private void fillChildrens(ItemProject itemProject, IconNodeProject node)
    {
        if(!itemProject.getName().equals("assets")
                && !itemProject.getName().equals("runtime"))
        {
            if (itemProject.getChildrens().size()>0)
            {
               for(ItemProject i : itemProject.getChildrens())
               {
                  boolean hasChildren = i.getChildrens().size() >0 ;
                  IconNodeProject newNode = new IconNodeProject(i, hasChildren, new javax.swing.ImageIcon(getClass().getResource("/pradoeditor/resources/icons/OpenProject.png")));

                  node.add(newNode);

                   if (i.getChildrens().size()>0)
                        fillChildrens(i, newNode);
               }
            }
        }
    }
  
    public DockKey getDockKey()
    {
        return key;
    }

    public Component getComponent()
    {
        return this;
    }

    public void initTree()
    {
        nodeTop = new IconNodeProject(LABEL_PROJECT, true, new javax.swing.ImageIcon(getClass().getResource("/pradoeditor/resources/icons/NewProject.png")));
        nodes.add(nodeTop);

        treeProject = new JTree( nodes.get(0));
        treeProject.setCellRenderer(new IconNodeRenderer());
        jScrollPane1.setViewportView(treeProject);
    }

    RequestOpenItemEventListener miReceptor;
    public void addRequestOpenItemEventListener(RequestOpenItemEventListener receptor )
    {
        // No se permite que intente incorporar mas de un receptor
        if( miReceptor == null )
            miReceptor = receptor;
    }

    // <editor-fold defaultstate="collapsed" desc="Definicion de Mensajes">

        private final String LABEL_TITLE = MessagesManager.getMessage(MessagesCodes.pradoeditor_projectexplorer_ProjectExplorer_LABEL_TITLE);
        private final String LABEL_PROJECT = MessagesManager.getMessage(MessagesCodes.pradoeditor_projectexplorer_ProjectExplorer_LABEL_PROJECT);
        
    // </editor-fold>

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        treeProject = new javax.swing.JTree();

        treeProject.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeProjectMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(treeProject);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void treeProjectMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeProjectMouseClicked

        //Clic derecho
        if(SwingUtilities.isRightMouseButton(evt))
         {
            int selectedRow = treeProject.getRowForLocation(evt.getX(), evt.getY());
            treeProject.setSelectionRow(selectedRow);
            
            TreePath selectedPath = treeProject.getPathForLocation(evt.getX(), evt.getY());

            if(selectedRow != -1)
            {
                DefaultMutableTreeNode tn = (DefaultMutableTreeNode)(selectedPath.getLastPathComponent());

                if (  tn.getUserObject() instanceof ItemProject)
                {
                    ItemProject selectedItemProject = ((ItemProject)(tn.getUserObject()));

                    PopupMenu popupMenu = PopupMenuFactory.createPopupMenu(selectedItemProject);
                    if(popupMenu != null)
                    {
                        popupMenu.show(treeProject,evt.getX(),evt.getY());
                        popupMenu.setVisible(true);
                    }
                }
            }
         }

         //Doble click
         if (evt.getClickCount()==2)
         {
            TreePath selectedPath = treeProject.getPathForLocation(evt.getX(), evt.getY());
            DefaultMutableTreeNode tn = (DefaultMutableTreeNode)(selectedPath.getLastPathComponent());
            ItemProject selectedItemProject = ((ItemProject)(tn.getUserObject()));

            //JOptionPane.showMessageDialog(null, selectedItemProject.getPath());

            miReceptor.OnRequestOpenItem(new RequestOpenItemEvent(this, selectedItemProject));
         }
    }//GEN-LAST:event_treeProjectMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree treeProject;
    // End of variables declaration//GEN-END:variables

}
